package com.tbit.main.controller;


import com.tbit.main.common.JsonResult;
import com.tbit.main.exception.BaseException;
import com.tbit.main.pojo.AgentSta;
import com.tbit.main.pojo.KVEntity;
import com.tbit.main.service.AgentStaService;
import com.tbit.main.service.TokenService;
import com.tbit.main.service.UserService;
import com.tbit.main.service.VipLogService;
import com.tbit.main.util.Assert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 代理商数据统计
 *
 * @author Leon
 * 2019年1月23日 下午3:51:50
 */
@RestController
@RequestMapping("/agentSta")
public class AgentStaController {
    private static final Logger log = LoggerFactory.getLogger(AgentStaController.class);
    /**
     * 金额格式化
     */
    private DecimalFormat df = new DecimalFormat("0.00");
    // 骑行订单金额日统计时间格式
    private static final String TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    @Autowired
    private AgentStaService agentStaService;
    @Autowired
    private UserService userService;
    @Autowired
    private VipLogService vipLogService;
    @Autowired
    private TokenService tokenService;

    /**
     * 根据代理商统计
     */
    @RequestMapping("/staByAccountId")
    public JsonResult send(String token, Integer accountId, String startTime, String endTime) {
        Assert.notNull(accountId, "AccountUser.agentId.notNull");
        Assert.hasLength(startTime, "System.startTime.notNull");
        Assert.hasLength(endTime, "System.endTime.notNull");

        AgentSta agentSta = agentStaService.staByTime(accountId, startTime, endTime);

        return JsonResult.succ(agentSta);
    }


    /**
     * 骑行订单量日统计
     */
    @RequestMapping("/orderCountGroupByDate")
    public JsonResult orderCountGroupByDate(String token, Integer accountId, String startTime, String endTime) {

        Assert.hasLength(startTime, "System.startTime.notNull");
        Assert.hasLength(endTime, "System.endTime.notNull");
        Assert.notNull(accountId, "AccountUser.agentId.notNull");

        // 校验时间参数格式 yyyy-MM-dd HH:mm:ss
        SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT);
        try {
            sdf.parse(startTime);
        } catch (Exception e) {
            log.error("骑行订单量日统计开始时间参数格式错误：{}", startTime);
            throw new BaseException("System.startTime.formatError");
        }
        try {
            sdf.parse(endTime);
        } catch (Exception e) {
            log.error("骑行订单量日统计结束时间参数格式错误：{}", endTime);
            throw new BaseException("System.endTime.formatError");
        }

        List<KVEntity> kvEntitys = agentStaService.orderCountGroupByDate(accountId, startTime, endTime);

        return JsonResult.succ(kvEntitys);
    }

    /**
     * 骑行订单金额日统计
     */
    @RequestMapping("/orderMoneyGroupByDate")
    public JsonResult orderMoneyGroupByDate(String token, Integer accountId, String startTime, String endTime) {

        Assert.hasLength(startTime, "System.startTime.notNull");
        Assert.hasLength(endTime, "System.endTime.notNull");
        Assert.notNull(accountId, "AccountUser.agentId.notNull");

        List<KVEntity> kvEntitys = agentStaService.orderMoneyGroupByDate(accountId, startTime, endTime);
        for (KVEntity kv : kvEntitys) {
            kv.setV(df.format(Double.parseDouble(kv.getV()) / 100));
        }

        return JsonResult.succ(kvEntitys);
    }


    /**
     * 免押订单量日统计
     */
    @RequestMapping("/memberCountGroupByDate")
    public JsonResult memberCountGroupByDate(String token, Integer accountId, String startTime, String endTime) {

        Assert.hasLength(startTime, "System.startTime.notNull");
        Assert.hasLength(endTime, "System.endTime.notNull");
        Assert.notNull(accountId, "AccountUser.agentId.notNull");

        List<KVEntity> kvEntitys = agentStaService.memberCountGroupByDate(accountId, startTime, endTime);

        return JsonResult.succ(kvEntitys);
    }

    /**
     * 免押订单金额日统计
     */
    @RequestMapping("/memberMoneyGroupByDate")
    public JsonResult memberMoneyGroupByDate(String token, Integer accountId, String startTime, String endTime) {

        Assert.hasLength(startTime, "System.startTime.notNull");
        Assert.hasLength(endTime, "System.endTime.notNull");
        Assert.notNull(accountId, "AccountUser.agentId.notNull");

        List<KVEntity> kvEntitys = agentStaService.memberMoneyGroupByDate(accountId, startTime, endTime);
        for (KVEntity kv : kvEntitys) {
            kv.setV(df.format(Double.parseDouble(kv.getV()) / 100));
        }

        return JsonResult.succ(kvEntitys);
    }


    /**
     * 骑行会员订单量日统计
     */
    @RequestMapping("/rideCardCountGroupByDate")
    public JsonResult rideCardCountGroupByDate(String token, Integer accountId, String startTime, String endTime) {

        Assert.hasLength(startTime, "System.startTime.notNull");
        Assert.hasLength(endTime, "System.endTime.notNull");
        Assert.notNull(accountId, "AccountUser.agentId.notNull");

        List<KVEntity> kvEntitys = agentStaService.rideCardCountGroupByDate(accountId, startTime, endTime);

        return JsonResult.succ(kvEntitys);
    }

    /**
     * 骑行会员订单金额日统计
     */
    @RequestMapping("/rideCardMoneyGroupByDate")
    public JsonResult rideCardMoneyGroupByDate(String token, Integer accountId, String startTime, String endTime) {

        Assert.hasLength(startTime, "System.startTime.notNull");
        Assert.hasLength(endTime, "System.endTime.notNull");
        Assert.notNull(accountId, "AccountUser.agentId.notNull");

        List<KVEntity> kvEntitys = agentStaService.rideCardMoneyGroupByDate(accountId, startTime, endTime);
        for (KVEntity kv : kvEntitys) {
            kv.setV(df.format(Double.parseDouble(kv.getV()) / 100));
        }

        return JsonResult.succ(kvEntitys);
    }


    /**
     * VIP会员订单量日统计
     */
    @RequestMapping("/vipCountGroupByDate")
    public JsonResult vipCountGroupByDate(String token, Integer accountId, String startTime, String endTime) {

        Assert.hasLength(startTime, "System.startTime.notNull");
        Assert.hasLength(endTime, "System.endTime.notNull");
        Assert.notNull(accountId, "AccountUser.agentId.notNull");

        List<KVEntity> kvEntitys = vipLogService.vipCountGroupByDate(accountId, startTime, endTime);

        return JsonResult.succ(kvEntitys);
    }

    /**
     * VIP会员订单金额日统计
     */
    @RequestMapping("/vipMoneyGroupByDate")
    public JsonResult vipMoneyGroupByDate(String token, Integer accountId, String startTime, String endTime) {

        Assert.hasLength(startTime, "System.startTime.notNull");
        Assert.hasLength(endTime, "System.endTime.notNull");
        Assert.notNull(accountId, "AccountUser.agentId.notNull");

        List<KVEntity> kvEntitys = vipLogService.vipMoneyGroupByDate(accountId, startTime, endTime);
        for (KVEntity kv : kvEntitys) {
            kv.setV(df.format(Double.parseDouble(kv.getV()) / 100));
        }

        return JsonResult.succ(kvEntitys);
    }

    /**
     * 订单时间分布统计
     */
    @RequestMapping("/staRideTime")
    public JsonResult staRideTime(String token, Integer accountId, String startTime, String endTime) {

        Assert.hasLength(startTime, "System.startTime.notNull");
        Assert.hasLength(endTime, "System.endTime.notNull");
        Assert.notNull(accountId, "AccountUser.agentId.notNull");

        List<KVEntity> kvEntitys = agentStaService.staRideTime(accountId, startTime, endTime);

        return JsonResult.succ(kvEntitys);
    }

    /**
     * 新用户分组统计
     */
    @RequestMapping("/newUserGroupByDate")
    public JsonResult newUserGroupByDate(String token, Integer accountId, String startTime, String endTime) {
        Assert.hasLength(startTime, "System.startTime.notNull");
        Assert.hasLength(endTime, "System.endTime.notNull");
        Assert.notNull(accountId, "AccountUser.agentId.notNull");

        List<KVEntity> kvEntitys = agentStaService.newUserGroupByDate(accountId, startTime, endTime);

        return JsonResult.succ(kvEntitys);
    }

    /**
     * 订单车辆比
     */
    @RequestMapping("/orderMachineRatio")
    public JsonResult orderMachineRatio(String token, Integer accountId, String startTime, String endTime) {

        Assert.hasLength(startTime, "System.startTime.notNull");
        Assert.hasLength(endTime, "System.endTime.notNull");
        Assert.notNull(accountId, "AccountUser.agentId.notNull");

        List<KVEntity> kvEntitys = agentStaService.orderMachineRatio(accountId, startTime, endTime);

        return JsonResult.succ(kvEntitys);
    }

    /**
     * 性别分布
     */
    @RequestMapping("/groupBySex")
    public JsonResult groupBySex(String token, Integer accountId) {
        Assert.notNull(accountId, "AccountUser.agentId.notNull");

        List<KVEntity> kvEntitys = userService.groupBySex(accountId);

        return JsonResult.succ(kvEntitys);
    }

    /**
     * 年龄段分布
     */
    @RequestMapping("/groupByAge")
    public JsonResult groupByAge(String token, Integer accountId) {
        Assert.notNull(accountId, "AccountUser.agentId.notNull");

        List<KVEntity> kvEntitys = userService.groupByAge(accountId);

        return JsonResult.succ(kvEntitys);
    }
}